package com.shengzhe.ostudent.controller.admin;

import cn.hutool.db.handler.StringHandler;
import com.shengzhe.ostudent.common.exception.OsException;
import com.shengzhe.ostudent.controller.base.BaseController;
import com.shengzhe.ostudent.entity.PageInfo;
import com.shengzhe.ostudent.entity.Role;
import com.shengzhe.ostudent.entity.RoleCondition;
import com.shengzhe.ostudent.entity.RoleTree;
import com.shengzhe.ostudent.service.RoleService;
import com.shengzhe.ostudent.service.impl.RoleServiceImpl;
import com.shengzhe.ostudent.utils.result.E;
import com.shengzhe.ostudent.utils.result.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/admin/role/*")
public class AdminRoleController extends BaseController {
    private final RoleService roleService = new RoleServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = super.getPathInfo();
        R r = R.ok();
        switch (pathInfo) {
            case "/list":
                System.out.println("/admin/role>>>doGet>>>list");
                RoleCondition roleCondition = null;
                List<Role> roleList = roleService.list(roleCondition);
                r.setEntityList(roleList);
                r.writeJson(resp);
                break;
            case "/listByCondition":
                System.out.println("/admin/role>>>doGet>>>listByCondition");
                roleCondition = super.getReqBean(RoleCondition.class);
                List<Role> roleListCondition = roleService.list(roleCondition);
                if (roleListCondition.isEmpty()) {
                    r.setMessage("查询结果为空，请核对查询条件");
                }
                r.setEntity(roleListCondition);
                r.writeJson(resp);
            case "/findById":
                long id = Long.parseLong(req.getParameter("id"));
                Role role = null;
                try {
                    role = roleService.findById(id);
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new OsException(E.USER_CODE_N50);
                }
                System.out.println(role);
                r.setEntity(role);
                r.writeJson(resp);
                break;
            case "/page":
                System.out.println("/admin/role>>>doGet>>>page");
                roleCondition = super.getReqBean(RoleCondition.class);
                PageInfo<Role> page = roleService.displayRolePage(roleCondition);
                r.setEntity(page);
                r.setSuccess(true);
                r.setMessage("成功");
                r.setCode(200);
                r.writeJson(resp);
                break;
            case "/tree":
//                Role role0Tree = super.getReqBean(Role.class);
                try {

                    RoleTree roleTree = roleService.buildRoleTree(roleService.findById(Long.parseLong(req.getParameter("id"))));
                    r.setEntity(roleTree);
                    r.writeJson(resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            default:
                System.out.println("/admin/role>>>doGet>>>default");
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = super.getPathInfo();
        R r = R.ok();
        switch (pathInfo) {
            case "/add":
                System.out.println("doPost>>add role");
                //通过工具类，来获取请求体的内容，返回值是字符串
                Role role = super.getBodyBean(Role.class);
                try {
                    roleService.add(role);
                } catch (SQLException e) {
                    throw new OsException(E.DATA_CODE_N6);
                }
                r.setEntity(role);
                r.writeJson(resp);
                break;
            case "/modify":
                System.out.println("AdminRoleController>>>doPost>>modify");
                Role rolefprUpdate = super.getBodyBean(Role.class);
                roleService.updateRole(rolefprUpdate);
                r.writeJson(resp);
                break;
            default:
                System.out.println("doGet>>>other");
                break;
        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = super.getPathInfo();
        R r = R.ok();
        switch (pathInfo) {
            case "/modify":
                System.out.println("AdminRoleController>>>doPut>>modify");
                Role role = super.getBodyBean(Role.class);
                roleService.modify(role);
                try {
                    r.setEntity(roleService.findById(role.getId()));
                    r.writeJson(resp);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            default:
                System.out.println("AdminRoleController>>>doPut>>>default");
        }
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doDelete(req, resp);
    }
}
